package drds.server.net;

import java.io.IOException;
import java.net.StandardSocketOptions;
import java.nio.channels.SocketChannel;

import drds.server.Server;
import drds.server.server.Session;
import drds.server.server.SessionContext;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class SessionContextFactory {

	public static ISessionContext make(SocketChannel socketChannel) throws IOException {
		socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
		socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

		ISessionContext sessionContext = getConnection(socketChannel);
		Server.getInstance().getConfig().setSocketParams(sessionContext);
		return sessionContext;
	}

	protected static SessionContext$LifeCycle getConnection(SocketChannel socketChannel) throws IOException {
		SessionContext sessionContext = new SessionContext(socketChannel);
		Server.getInstance().getConfig().setSocketParams(sessionContext);
		/**
		 * 创建session
		 */
		sessionContext.setSession(new Session(sessionContext));
		return sessionContext;
	}

}